<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" v-auth="'service_provider:product:edit'" icon="el-icon-plus"
						@click="handleData('add')">新建商品</el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.provider_product_id" placeholder="服务商商品ID" clearable></el-input>
						<el-select v-model="search.provider_id" placeholder="服务商" style="min-width:25%;" clearable>
							<el-option v-for="item in providerList" :key="item.provider_id" :label="item.provider_name"
								:value="item.provider_id">
							</el-option>
						</el-select>
						<el-select v-model="search.status" placeholder="状态" style="min-width:19%;" clearable>
							<el-option v-for="item in statusList" :key="item.status" :label="item.status_name"
								:value="item.status">
							</el-option>
						</el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="resetsearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
					remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="商品ID" prop="product_id" width="100"></el-table-column>
					<el-table-column label="服务商商品ID" prop="provider_product_id" width="150"></el-table-column>
					<el-table-column label="商品名称" prop="product_name" width="150"></el-table-column>
					<el-table-column label="商品状态" prop="status_name" width="150"></el-table-column>
					<el-table-column label="是否绿通服务" prop="is_green" width="100"
						:formatter="isGreenFormatter"></el-table-column>
					<el-table-column label="服务等待期" prop="wait_day" width="100"></el-table-column>
					<el-table-column label="采购价" prop="purchase_price" width="150"></el-table-column>
					<el-table-column label="销售价" prop="sale_price" width="150"></el-table-column>
					<el-table-column label="采购数量" prop="purchase_qty" width="150"></el-table-column>
					<el-table-column label="库存数量" prop="stock_qty" width="150"></el-table-column>
					<el-table-column label="已消耗数量" prop="use_qty" width="150"></el-table-column>
					<el-table-column label="有效期" prop="valid_date_text" width="140"></el-table-column>
					<el-table-column label="服务商名称" prop="provider_name" width="150"></el-table-column>
					<el-table-column label="状态" prop="status" width="80">
						<template #default="scope">
							<el-tag v-if="scope.row.status == 1" type="success">上架</el-tag>
							<el-tag v-if="scope.row.status == 2" type="danger">下架</el-tag>
							<el-tag v-if="scope.row.status == 3" type="danger">删除</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" prop="created_at" width="170"></el-table-column>
					<el-table-column label="创建人" prop="created_by" width="170"></el-table-column>
					<!--					<el-table-column label="启用停用" prop="status" width="80">-->
					<!--						<template #default="scope">-->
					<!--							<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)"-->
					<!--									   :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>-->
					<!--						</template>-->
					<!--					</el-table-column>-->
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<!--								<el-button text type="primary" size="small"-->
								<!--										   @click="handleData('show',scope.row, scope.$index)">查看-->
								<!--								</el-button>-->
								<el-button text type="primary" v-auth="'service_provider:product:edit'" size="small"
									@click="handleData('edit', scope.row, scope.$index)">编辑
								</el-button>
								<el-button v-if="scope.row.status == 1" text type="primary"
									v-auth="'service_provider:product:edit'" size="small"
									@click="changeStatus('change_status', scope.row, scope.$index)">下架
								</el-button>
								<el-button v-if="scope.row.status == 2" text type="primary"
									v-auth="'service_provider:product:edit'" size="small"
									@click="changeStatus('change_status', scope.row, scope.$index)">上架
								</el-button>
								<el-button v-if="scope.row.status == 2" text type="primary"
									v-auth="'service_provider:product:edit'" size="small"
									@click="changeStatus('delete', scope.row, scope.$index)">删除
								</el-button>
								<!--								<el-button v-if="scope.row.status==0" text type="primary" v-auth="'service_provider:index:delete'" size="small"-->
								<!--										   @click="deleteProvider('delete',scope.row, scope.$index)">删除-->
								<!--								</el-button>-->
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess"
		@closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from './product_save.vue'

export default {
	name: 'serviceProviderProduct',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			showGroupLoading: false,
			loading: false,
			apiObj: this.$API['service_provider.service_provider'].productIndex,
			selection: [],
			statusList: [
				{ status: 1, status_name: "上架" },
				{ status: 2, status_name: "下架" },
			],
			providerList: [],
			search: {
				provider_id: null,
				provider_product_id: null,
				status: null,
			}
		}
	},
	mounted() {
		//获取单位列表
		this.getProviderList()
	},
	methods: {
		isGreenFormatter(row) {
			if (row.is_green == 1) {
				return "是"
			} else {
				return "否"
			}
		},
		getProviderList() {
			this.$API['service_provider.service_provider'].getProviderList.get().then((res) => {
				if (res.code == 0) {
					this.providerList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", { type: 'error' })
			})
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		handleData(mode, row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open(mode).setData(row)
			})
		},
		//启用停用
		changeStatus(mode, row) {
			console.log(mode, row);
			var status = row['status'];
			var tip = "";
			var val = "";
			if (status == 1) {
				tip = "确认下架当前服务商商品吗？确认后变更为下架";
				val = 2;
			}
			if (status == 2) {
				if (mode == 'delete') {
					tip = "确认删除当前服务商商品吗？确认后变更为删除";
					val = 3;
				} else {
					tip = "确认上架当前服务商商品吗？确认后变更为上架";
					val = 1;
				}
			}
			this.$confirm(tip, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['service_provider.service_provider'].changeProductStatus.post({ product_id: row.product_id, status: val })
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", { type: 'error' })
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", { type: 'error' })
					})
					.finally(() => {

					});
			}).catch(() => {
			})

		},
		//删除
		deleteProvider(mode, row) {
			console.log(mode, row);
			this.$confirm(`注：确认删除此用户吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['user.user'].deleteUser.post({ provider_id: row.provider_id })
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", { type: 'error' })
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", { type: 'error' })
					})
					.finally(() => {

					});
			}).catch(() => {
			})

		},

		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//表格内开关
		changeSwitch(val, row) {
			row.$switch_status = true;
			this.$API['user.user'].changeStatus.post({ status: val, provider_id: row.provider_id })
				.then((res) => {
					if (res.code == 0) {
						this.$emit('success', this.form, this.mode)
						this.$message.success("操作成功")
						row.status = val;
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				})
				.catch((msg) => {
					this.$alert(msg, "提示", { type: 'error' })
				})
				.finally(() => {
					row.$switch_status = false;
				});
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//重置
		resetsearch() {
			this.search = {
				provider_id: null,
				provider_product_id: null,
				status: null,
			}
		},
		//本地更新数据
		handleSuccess() {
			this.dialog.save = false
			this.$refs.table.reload()
		},
	}
}
</script>

<style></style>
